<template>
  <div class="app-container">
    <el-form :inline="true" @submit.native.prevent ref="queryForm" :model="queryParams">
      <el-form-item label="" prop="remarks">
        <el-input
          v-model="queryParams.remarks"
          placeholder="输入关键字进行查询"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary" plain
          icon="el-icon-upload2"
          size="mini"
          @click="uploadTool"
        >上传工具</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="tableList">
      <el-table-column
        label="类别"
        prop="fileType"
        :show-overflow-tooltip="true"
        width="300"
      />
      <el-table-column
        label="文件名"
        prop="fileName"
        :show-overflow-tooltip="true"
        width="300"
      />
      <el-table-column
        label="说明"
        prop="remarks"
        :show-overflow-tooltip="true"
        width="400"
      />
      <el-table-column label="上传时间"  prop="createTime" width="160" />
      <el-table-column label="操作"  class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            icon="el-icon-view"
            @click="previewFile(scope.row)"
            v-if="enableReview(scope.row)"
          >预览</el-button>
          <el-button
            type="text"
            size="small"
            icon="el-icon-download"
            @click="downloadFile(scope.row)"
          >下载</el-button>
          <el-button
            type="text"
            size="small"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 用户导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">
          将文件拖到此处，或<em>点击上传</em>
        </div>
        <div class="el-upload__tip" slot="tip">
          <el-form :model="upload" :inline="true" @submit.native.prevent :rules="rules" ref="uploadForm">
            <el-form-item label="类别" prop="fileType">
              <el-select v-model="upload.fileType" placeholder="请选择类别">
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="说明" prop="remarks">
              <el-input type="textarea" v-model="upload.remarks" placeholder="请输入说明" size="medium"></el-input>
            </el-form-item>
          </el-form>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :visible.sync="dialogVisible">
      <div style="max-height: 90vh; overflow-y: scroll; text-align: center;">
        <img :src="dialogImageUrl" alt="" />
      </div>
    </el-dialog>

  </div>
</template>

<script>
  import { getToken } from '@/utils/auth'
  import { queryFile, updateFile, deleteFile } from '@/api/basic/file.js'

  export default {
    name: "importExcel",
    components: {},
    data() {
      return {
        // 用户导入参数
        upload: {
          // 是否显示弹出层（用户导入）
          open: false,
          // 弹出层标题（用户导入）
          title: "",
          // 是否禁用上传
          isUploading: false,
          // 是否更新已经存在的用户数据
          updateSupport: 0,
          // 设置上传的请求头部
          headers: { Authorization: "Bearer " + getToken() },
          // 上传的地址
          url: '',
          remarks: "",
          fileType: "业务模板"
        },
        rules: {
          //remarks: [{ required: true, message: "说明不能为空", trigger: "blur" }],
        },
        queryParams: {
          remark: null,
          fileTypes: "业务模板,系统工具,运维工具",
          pageNum: 1,
          pageSize: 10,
        },
        loading: false,
        tableList: [],
        total: 0,
        dialogVisible: false,
        dialogImageUrl: null,
        options:  [{
          value: '业务模板',
          label: '业务模板'
        }, {
          value: '系统工具',
          label: '系统工具'
        },{
          value: '运维工具',
          label: '运维工具'
        }, ],
      };
    },
    created() {
      this.getList();
    },
    activated() {
    },
    methods: {
      /** 导入按钮操作 */
      uploadTool() {
        this.upload.url = process.env.VUE_APP_BASE_API + "/system/upload",
        this.upload.title = "文件上传";
        this.upload.open = true;
        this.upload.remarks = "";
      },
      // 提交上传文件
      submitFileForm() {
        this.$refs["uploadForm"].validate(valid => {
          if (valid) {
            this.$refs.upload.submit();
          }
        });
      },
      // 文件上传中处理
      handleFileUploadProgress(event, file, fileList) {
        this.upload.isUploading = true;
      },
      // 文件上传成功处理
      handleFileSuccess(response, file, fileList) {
        if(!response.success){
          this.$message.error(response.msg);
          return;
        }
        let uploadFile = response.data;
        uploadFile.fileType = this.upload.fileType;
        uploadFile.remarks = this.upload.remarks;
        updateFile(uploadFile).then(res => {
          if(!res.success){
            this.$message.error(response.msg);
            return;
          }
          this.upload.open = false;
          this.upload.isUploading = false;
          this.$refs.upload.clearFiles();
          this.$message.success(response.msg);
          this.getList();
        });
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 查询表集合 */
      getList() {
        this.loading = true;
        queryFile(this.queryParams).then(response => {
          this.tableList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        this.$confirm('是否确认删除文件?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return deleteFile(row.id);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      downloadFile(item){
        let url = process.env.VUE_APP_BASE_API + "/common/download/resource?filePath=" + encodeURI(item.filePath) + "&downloadName="+encodeURI(item.fileName)+"&delete=" + false;
        window.open(url);
      },
      enableReview(item){
        let ext = item.fileName.substring(item.fileName.lastIndexOf(".") + 1, item.fileName.length);
        return (ext == "gif" || ext== "jpeg" || ext== "jpg" || ext == "png");
      },
      previewFile(item) {
        this.dialogImageUrl = process.env.VUE_APP_BASE_API + item.filePath;
        this.dialogVisible = true;
      },
    },
  };
</script>
<style scoped lang="less">
</style>
